package com.boco.modules.fdoc.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.boco.common.constants.BusinessConstants;
import com.boco.common.persistence.Page;
import com.boco.common.utils.NumberUtils;
import com.boco.modules.fdoc.dao.FamdoctorDao;
import com.boco.modules.fdoc.dao.SigServicepackDao;
import com.boco.modules.fdoc.dao.UserDao;
import com.boco.modules.fdoc.dao.UserRelationsDao;
import com.boco.modules.fdoc.model.UserDocSignEntity;
import com.boco.modules.fdoc.model.UserEntity;
import com.boco.modules.fdoc.service.SignService;
import com.boco.modules.fdoc.vo.ResidentVo;
import com.boco.modules.fdoc.vo.SigServicepackVo;
import com.boco.modules.fdoc.vo.SignVo;

@Service
public class SignServiceImpl implements SignService{

	@Resource
	FamdoctorDao famdocDao;
	@Resource
	UserRelationsDao userRelationsDao;
	@Resource
	UserDao userDao;
	@Resource
	SigServicepackDao servicepackDao;
	
	@Override
	public SignVo getSignNum(String uid) {
		SignVo vo = new SignVo();
		Integer signNum = famdocDao.getSignedNum(uid);
		vo.setSignedNum(signNum);
		Integer notSignNum = famdocDao.getNotSignedNum(uid);
		vo.setNotSignNum(notSignNum);
		
		vo.setSignedPer(NumberUtils.roundHalfUp((double)signNum*100/(signNum+notSignNum), 0));
		vo.setNotSignPer(100-vo.getSignedPer());
		return vo;
	}
	@Override
	public List<ResidentVo> getSignedList(String uid) {
		return famdocDao.getSignedList(uid);
	}
	@Override
	public List<ResidentVo> getNotSignedList(String uid) {
		return famdocDao.getNotSignedList(uid);
	}
	@Override
	public Integer sign(UserDocSignEntity en) {
		int famdocId = famdocDao.getByUid(en.getDocUid()).getId();
		en.setFamdocId(famdocId);
		en.setStatus(BusinessConstants.SIGN_STATUS_SIGNED); //设置为已签约
		en.setSignTime(new Date());
		en.setIsNoticed(BusinessConstants.SIGN_NOTICED_NO); //设置为未通知
		famdocDao.sign(en);
		
		String idCard = en.getIdCard();
		userDao.updateTypeByIdCard(idCard); //修改用户为签约用户
		return en.getId();
	}
	@Override
	public List<ResidentVo> getRelationsNotSigned(String idCard) {
		return famdocDao.getRelationsNotSigned(idCard);
	}
	@Override
	public Boolean isSigned(String uid) {
		UserEntity user = userDao.get(uid);
		Integer num = famdocDao.isSigned(user.getIdCard());
		if (num == 0) {
			return false;
		}else {
			return true;
		}
	}
	@Override
	public SignVo getSignDetail(String uid) {
		UserEntity loginUser = userDao.get(uid);
		SignVo vo = famdocDao.getABnameByLoginUid(uid);
		List<UserDocSignEntity> list = famdocDao.getFamilyPicByIdCard(loginUser.getIdCard());
		StringBuffer picStrBuffer = new StringBuffer();
		//拼接图片信息
		for (UserDocSignEntity userDocSignEntity : list) {
			if (userDocSignEntity.getSignImgHead()!=null) {
				picStrBuffer.append(userDocSignEntity.getSignImgHead());
				picStrBuffer.append("===");
			}
		}
		//封装签约者所签服务包及服务包详情
		SigServicepackVo servicepack = servicepackDao.getServicepackByIdCard(loginUser.getIdCard());
		if (servicepack != null) {
			servicepack.setResidentName(loginUser.getRealName());
			servicepack.setIdCard(loginUser.getIdCard());
			servicepack.setDetails(servicepackDao.getServicepackDetail(servicepack.getId()));
		}
		vo.setServicepacks(servicepack);
		vo.setPicStr(picStrBuffer.toString());
		return vo;
	}

	
}
